package com.yyk.yiliao.ui.mine.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.bean.MymessageRecordInfo;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.TimeUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 挂号记录Fragment   1
 */
public class Registraion_Fragment extends BaseFragment {

    public static final String ARGS_PAGE = "Registraion_One_Fragment";
    @BindView(R.id.nOne_rv)
    RecyclerView nOneRv;
    @BindView(R.id.nOne_tv)
    TextView nOneTv;
    private int pageIndex;

    private String mPage;
    private BaseRecyclerAdapter<MymessageRecordInfo.DataBean> adapter;
    private List<MymessageRecordInfo.DataBean> registrationList = new ArrayList<>();

    public static Registraion_Fragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        Registraion_Fragment fragment = new Registraion_Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_registraion;
    }

    @Override
    public void initView() {
        initAdapter();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        super.onFragmentVisibleChange(isVisible);
        if (isVisible) {
            //TODO 在这里处理数据
            initMymessageRecord(true);
        }
    }

    @Override
    public void initData() {
        Bundle arguments = getArguments();
        mPage = arguments.getString(ARGS_PAGE);
        Logger.d("滑动页数 initData" + mPage);

    }

    private void initMymessageRecord(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("user_id", Hawk.get("uid", 0) + "");
        map.put("registration_stat", mPage + "");

        map.put("sign", Md5Util2.createSign(map));
        Logger.d("滑动页数" + mPage);
        RxUtils.getInstance().retrofit.create(ApiService.class).postMymessageRecord(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<MymessageRecordInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MymessageRecordInfo info) {
                        Logger.e("挂号记录" + info.toString());

                        if (info.getCode() == 1) {
                            nOneTv.setVisibility(View.GONE);
                            nOneRv.setVisibility(View.VISIBLE);
                            if (flag) {
                                registrationList.clear();
                            }
                            if (info.getData().size() > 0 && info.getData() != null) {
                                registrationList.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            nOneTv.setVisibility(View.VISIBLE);
                            nOneRv.setVisibility(View.GONE);
                        }
                    }

                });

    }

    /**
     * 1. 挂号记录适配器
     */

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<MymessageRecordInfo.DataBean>(getActivity(), registrationList, R.layout.adapter_item_registraion) {
            @Override
            public void convert(BaseRecyclerHolder holder, MymessageRecordInfo.DataBean item, int position, boolean isScrolling) {
                //转换时间
                String time = TimeUtil.getInstance().getTime(item.getRegistration_time(), TimeUtil.YYYY_MM_DD);
                holder.setText(R.id.b_registration_time, "预约时间：" + time);

                holder.setText(R.id.b_department_name, "预约科室：" + item.getDepartment_name());

                holder.setText(R.id.b_true_name, "出诊医生：" + item.getTrue_name());

                holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPicture());

                int registration_timeslot = item.getRegistration_timeslot(); //预约时间段
                if (registration_timeslot == 1) {
                    holder.setText(R.id.b_registration_timeslot, "8:00-12:00");
                } else if (registration_timeslot == 2) {
                    holder.setText(R.id.b_registration_timeslot, "13:00-18:00");
                } else {
                    holder.setText(R.id.b_registration_timeslot, "17:00");
                }
                holder.setText(R.id.b_registration_amount, "已付挂号费：" + item.getRegistration_amount() + "元");

                Button b_exit = holder.getView(R.id.b_exit);

                //判断 按钮的类型
                switch (mPage) {
                    case "1":
                        b_exit.setBackgroundResource(R.mipmap.btn_selected);
                        b_exit.setText("退号");
                        b_exit.setTextColor(getResources().getColor(R.color.blue_4e));

                        break;
                    case "2":
                        b_exit.setBackgroundResource(R.mipmap.btn_disabled);
                        b_exit.setText("已使用");
                        b_exit.setTextColor(getResources().getColor(R.color.gray_80));
                        break;
                    case "3":
                        b_exit.setBackgroundResource(R.mipmap.btn_disabled);
                        b_exit.setText("已过期");
                        b_exit.setTextColor(getResources().getColor(R.color.gray_80));

                        break;
                    case "4":
                        b_exit.setBackgroundResource(R.mipmap.btn_disabled);
                        b_exit.setText("已退号");
                        b_exit.setTextColor(getResources().getColor(R.color.gray_80));

                        break;
                }
                //判断类型 不同的点击事件
                b_exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (mPage) {
                            case "1":
//                                showNormalDialog();
                                break;
                            case "2":

                                break;
                            case "3":
                                break;
                            case "4":
                                break;
                        }
                    }
                });
            }
        };
        nOneRv.addItemDecoration(new CustomDecoration(getActivity(), CustomDecoration.VERTICAL_LIST, R.drawable.divider_eb_1px, 0));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        nOneRv.setLayoutManager(linearLayoutManager);
        nOneRv.setAdapter(adapter);
//        nOneRv.addOnScrollListener(new EndLessOnScrollListener(linearLayoutManager) {
//            @Override
//            public void onLoadMore(int currentPage) {
////                if (pageIndex == currentPage) {
////                    return;
////                }
//                new Handler().postDelayed(new Runnable() {
//                    public void run() {
//                        //请求网络
//                        pageIndex++;
//                        initMymessageRecord(true);
//
//                    }
//                }, 1000);
//            }
//        });

    }


    private void showNormalDialog() {
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(getActivity());
//        //设置title
//        normalDialog.setTitle(getString(R.string.dialog_normal_text));
        //设置icon
//        normalDialog.setIcon(R.mipmap.ic_launcher_round);
        //设置内容
        normalDialog.setMessage("您确定要取消本次预约挂号么？取消预约挂号后挂号费用会在7个工作日内原途径返回您的付款账户。");
        //设置按钮
        normalDialog.setPositiveButton("确定"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "确定"
                                , Toast.LENGTH_SHORT).show();
                        registrationList.clear();
                        nOneTv.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();

                        dialog.dismiss();
                    }
                });
        //设置按钮
        normalDialog.setNegativeButton("再想想"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        which = Gravity.LEFT;
                        Toast.makeText(getActivity(), "再想想"
                                , Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }


}
